﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LibraryManager
{
    public partial class LockUserForm : Form
    {
        private String _id = "";
        public LockUserForm()
        {
            InitializeComponent();
        }

        private void FindAll()
        {
            string sql = "select uId,uName,uGender,uPhoNum,uBan from users where uBan = '1'";
            MyDictionary dic = new MyDictionary();
            DataTable dt = SqlHelper.GetList(sql, dic);
            dataGridView1.DataSource = dt;
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                dataGridView1.Rows[i].Cells["uBan"].Value = "是";
            }
        }

        private void LockUserForm_Load(object sender, EventArgs e)
        {
            FindAll();
        }

        private void FindLockUserBt_Click(object sender, EventArgs e)
        {
            string sql = "select uId,uName,uGender,uPhoNum,uBan from users where uBan = '1' and uId = @uId";
            MyDictionary dic = new MyDictionary();
            dic.Add("@uId", IdTextBox.Text.Trim());
            DataTable dt = SqlHelper.GetList(sql, dic);
            dataGridView1.DataSource = dt;
        }

        private void RefreshBt_Click(object sender, EventArgs e)
        {
            FindAll();
        }

        private void UnlockBt_Click(object sender, EventArgs e)
        {
            if (_id.Equals(""))
            {
                MessageBox.Show("请选择需解锁用户！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                DialogResult result = MessageBox.Show("确定解锁用户" + _id + "吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    String sql = "update users set uBan = '0' where uId = '" + _id + "'";
                    string str = System.Configuration.ConfigurationManager.ConnectionStrings["library"].ConnectionString;
                    SqlConnection conn = null;
                    try
                    {
                        conn = new SqlConnection(str);
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        // 打开数据库连接
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("已解锁用户" + _id);
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show(ee.Message.ToString());
                    }
                    finally
                    {
                        if (conn != null)
                        {
                            //关闭数据库连接
                            conn.Close();
                        }
                    }
                    FindAll();
                }
                else { return; }
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int a = dataGridView1.CurrentRow.Index;
            _id = dataGridView1.Rows[a].Cells["uId"].Value.ToString();
        }
    }
}
